#if __Include_AppLogging__

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.SharePoint;

namespace Dhs.Ice.Sharepoint.Utilities
{
    public class AppLogging 
    {
        const string ERROR_SOURCE = "ICESharepoint";

        /// <summary>
        /// Logs an error to the system event log
        /// </summary>
        /// <param name="e"></param>
        public void LogError(Exception e)
        {
            LogError(e,ERROR_SOURCE);
        }

        public void LogError(Exception e, string source)
        {
            string message = BuildErrorMessage(e);
            EventLog.WriteEntry(source,message, EventLogEntryType.Error);
        }

        private string BuildErrorMessage(Exception e)
        {
            if (e == null) return string.Empty;
            string errorText = string.Format("Error: {0} \r\n Stack:\r\n{1}\r\n",e.Message,e.StackTrace);
            errorText += BuildErrorMessage(e.InnerException);
            return errorText;
        }

        public void LogError(string message)
        {
            LogError(message,ERROR_SOURCE);
        }

        public void LogError(string message, string source)
        {
            EventLog.WriteEntry(source,message);
        }

        public void LogMessage(string message)
        {
            LogMessage(message,EventLogEntryType.Information,ERROR_SOURCE);
        }

        public void LogMessage(string message, EventLogEntryType entryType)
        {
          LogMessage(message,entryType,ERROR_SOURCE);
        }

        public void LogMessage(string message, string source)
        {
            LogMessage(message, EventLogEntryType.Information, source);
        }

        public void LogMessage(string message, EventLogEntryType entryType, string source)
        {
            EventLog.WriteEntry(source, message, entryType);
        }

        public void AuditSPEvent(SPWeb web, SPAuditEventType eventType,string source, string xmlData)
        {
            try
            {
                web.Audit.WriteAuditEvent(eventType, source, xmlData);
            }
            catch
            {
                LogMessage(string.Format("Unable to write audit log info: {0}", xmlData), "ICEAuditing");
            }
        }
    }
}

#endif